package com.happyscan138.makefun;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.happyscan138.makefun.BaseAdapter.systemNews_listview;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.systemNewsAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.happyscan138.makefun.base.share.access_key;

/**
 * Created by Administrator on 2016/12/20.
 */

public class MypersionalNews extends AppCompatActivity {
    private ListView listView_sysstemNews;
    private List<systemNews_listview> listdata;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_news);
        preferences=getSharedPreferences("UserInfo",MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView();
        getAdapterView();
    }

    public void initView(){
        listView_sysstemNews=(ListView) findViewById(R.id.news_listview);
    }
    public void getAdapterView(){
        client=new AsyncHttpClient();
        params=new RequestParams();
        params.put("access_key",access_key);
        final systemNews_listview data=new systemNews_listview();
        listdata=new ArrayList<>();
        RequestHandle handle=client.post(Urls.SystemNews,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject obj=jsonArray.getJSONObject(i);
                        data.head=base+obj.getString("img_path");
                        data.name=obj.getString("nick_name");
                        data.time=obj.getString("post_date");
                        listdata.add(data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


        listView_sysstemNews.setAdapter(new systemNewsAdapter(this,listdata));

    }
}
